package sdstcom.lbq.one.lbq.uilts;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Administrator on 2016/12/13 0013.
 */
public class HttpUtils {
    public HttpUtils() {
    }

    public static byte[] getRes(String url) {
        HttpURLConnection huc = null;
        BufferedInputStream bis = null;

        try {
            URL e = new URL(url);
            huc = (HttpURLConnection)e.openConnection();
            huc.setConnectTimeout(3000);
            huc.connect();
            if(huc.getResponseCode() != 200) {
                return null;
            } else {
                bis = new BufferedInputStream(huc.getInputStream());
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] data = new byte[1024];

                while(true) {
                    int len = bis.read(data);
                    if(len == -1) {
                        byte[] var8 = baos.toByteArray();
                        return var8;
                    }

                    baos.write(data, 0, len);
                }
            }
        } catch (Exception var17) {
            Log.e("HttpUtils", var17.toString());
            return null;
        } finally {
            if(bis != null) {
                try {
                    bis.close();
                } catch (IOException var16) {
                    var16.printStackTrace();
                }
            }

            if(huc != null) {
                huc.disconnect();
            }

        }
    }

    public static String getJSONStr(String url, String... encodingName) {
        HttpURLConnection huc = null;
        BufferedInputStream bis = null;

        try {
            URL e = new URL(url);
            huc = (HttpURLConnection)e.openConnection();
            huc.setConnectTimeout(3000);
            huc.connect();
            if(huc.getResponseCode() != 200) {
                return null;
            } else {
                bis = new BufferedInputStream(huc.getInputStream());
                StringBuilder builder = new StringBuilder();
                byte[] data = new byte[1024];

                while(true) {
                    int len = bis.read(data);
                    if(len == -1) {
                        String var9 = builder.toString();
                        return var9;
                    }

                    if(encodingName.length == 0) {
                        builder.append(new String(data, 0, len));
                    } else {
                        builder.append(new String(data, 0, len, encodingName[0]));
                    }
                }
            }
        } catch (Exception var18) {
            Log.e("HttpUtils", var18.toString());
            return null;
        } finally {
            if(bis != null) {
                try {
                    bis.close();
                } catch (IOException var17) {
                    var17.printStackTrace();
                }
            }

            if(huc != null) {
                huc.disconnect();
            }

        }
    }

    public static Bitmap getBitMap(String url) {
        HttpURLConnection huc = null;
        BufferedInputStream bis = null;

        try {
            URL e = new URL(url);
            huc = (HttpURLConnection)e.openConnection();
            huc.setConnectTimeout(3000);
            huc.connect();
            if(huc.getResponseCode() == 200) {
                bis = new BufferedInputStream(huc.getInputStream());
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] data = new byte[1024];

                while(true) {
                    int len = bis.read(data);
                    if(len == -1) {
                        byte[] img = baos.toByteArray();
                        Bitmap var9 = BitmapFactory.decodeByteArray(img, 0, img.length);
                        return var9;
                    }

                    baos.write(data, 0, len);
                }
            }
        } catch (Exception var18) {
            Log.e("HttpUtils", var18.toString());
        } finally {
            if(bis != null) {
                try {
                    bis.close();
                } catch (IOException var17) {
                    var17.printStackTrace();
                }
            }

            if(huc != null) {
                huc.disconnect();
            }

        }

        return null;
    }
}
